package com.liu.serialization.impl;


import com.liu.serialization.Serialization;
import com.liu.serialization.SerializationTypeEnum;

import java.nio.charset.StandardCharsets;

/**
 * @param
 * @BelongsProject: MessageQueueDEMO
 * @BelongsPackage: com.liu.util
 * @author: L
 * @Date: 2023/10/13 10:59
 * @return
 */
public class JSONType implements Serialization {
    @Override
    public Boolean support(SerializationTypeEnum serializationType) {
        return serializationType == SerializationTypeEnum.JSON;
    }

    @Override
    public <T> T deserialization(Class<T> clazz, byte[] object) {
        // String str = new String((byte[]) object, StandardCharsets.UTF_8);
        return com.alibaba.fastjson.JSON.parseObject((byte[])object, clazz);
    }

    @Override
    public byte[] serialization(Object object) {
        String jsonString = com.alibaba.fastjson.JSON.toJSONString(object);
        return jsonString.getBytes(StandardCharsets.UTF_8);
        // return com.alibaba.fastjson.JSON.toJSONString(object);
    }
}
